package org.com.android.polo;

import java.util.HashMap;
import java.util.Map;

import org.com.android.constant.Constant;
import org.com.android.utility.Utility;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class setting extends Activity {

	
	private Button ok;
	private Button cancel;
	
	private EditText username ;
	private EditText server ;
	private EditText password ;
	private EditText passwordvalidator ;
	private Spinner entity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		entity = (Spinner) findViewById(R.id.entity);
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,
				android.R.layout.simple_spinner_dropdown_item, new String[] {
						getString(R.string.changzhou),
						getString(R.string.hangzhou),
						getString(R.string.nanjing),
						getString(R.string.ningbo),
						getString(R.string.shanghai) });
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		entity.setAdapter(adapter);

		ok = (Button) findViewById(R.id.ok);
		cancel = (Button) findViewById(R.id.cancel);
		
		username = (EditText)findViewById(R.id.username) ;
		password = (EditText)findViewById(R.id.password) ;
		server   = (EditText)findViewById(R.id.server) ;
		passwordvalidator = (EditText)findViewById(R.id.passwordvalidator) ;

		ok.setOnClickListener(onok) ;
		Map<String,String> conf = Utility.readconf(getSharedPreferences(Constant.CONF,0));
		username.setText(conf.get("username"));
		password.setText(conf.get("password"));
		passwordvalidator.setText(conf.get("password"));
		server.setText(conf.get("server"));
		entity.setSelection(0);
	}
	
	
	private Button.OnClickListener onok = new Button.OnClickListener() {
		public void onClick(View v) {			
			
			String p = password.getText().toString();
			String pv= passwordvalidator.getText().toString();
			String u = username.getText().toString();
			String s = server.getText().toString() ;
			int e = (int)entity.getSelectedItemId();
			
			if (p == null || p.length() == 0){
				return ;
			}
			if (pv== null || pv.length()== 0){
				return ;
			}
			if (p.compareTo(pv) != 0){
				return ;
			}
			if (u == null || u.length() == 0){
				return ;
			}
			if (s == null || s.length() == 0){
				return ;
			}
			
			String [] array = {"changzhou","nanjing","ningbo","shanghai"};
			Map<String, String> conf = new HashMap<String,String>() ;
			conf.put("password", p) ;
			conf.put("username", u) ;
			conf.put("server", s) ;
			conf.put("entity", array[e]);
			Utility.saveconf(getSharedPreferences(Constant.CONF,0),conf);
		}
	};
	
	

}